﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Products
{
    /// <summary>
    /// This object represents the methods of a Product.
    /// </summary>
    internal class ProductDAO
    {

        #region general
        private static Product LoadObj(IDataReader datareader)
        {
            Product obj = new Product();
            obj.ProductID = MpDataHelp.GetInt(datareader, "ProductID");
            obj.CategoryID = MpDataHelp.GetInt(datareader, "CategoryID");
            obj.ManufacturerID = MpDataHelp.GetInt(datareader, "ManufacturerID");
            obj.SortOrder = MpDataHelp.GetInt(datareader, "SortOrder");
            obj.Price = MpDataHelp.GetDouble(datareader, "Price");
            obj.PriceNew = MpDataHelp.GetDouble(datareader, "PriceNew");
            obj.ProductCode = MpDataHelp.GetString(datareader, "ProductCode");
            obj.ProductName = MpDataHelp.GetString(datareader, "ProductName");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Content = MpDataHelp.GetString(datareader, "Content");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            obj.FormatUrl = MpDataHelp.GetString(datareader, "FormatUrl");
            obj.Images = MpDataHelp.GetString(datareader, "Images");
            obj.IsExist = MpDataHelp.GetBoolean(datareader, "IsExist");
            obj.IsActive = MpDataHelp.GetBoolean(datareader, "IsActive");
            obj.IsVAT = MpDataHelp.GetBoolean(datareader, "IsVAT");
            obj.IsHot = MpDataHelp.GetBoolean(datareader, "IsHot");
            obj.IsHome = MpDataHelp.GetBoolean(datareader, "IsHome");
            obj.IsSellGood = MpDataHelp.GetBoolean(datareader, "IsSellGood");
            obj.IsPromotion = MpDataHelp.GetBoolean(datareader, "IsPromotion");
            obj.DateCreate = MpDataHelp.GetDateTime(datareader, "DateCreate");

            return obj;
        }
        #endregion

        public static int Product_Add(Product obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Product_Add");
            db.AddOutParameter(dbCommand, "ProductID", DbType.Int32, obj.ProductID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "ManufacturerID", DbType.Int32, obj.ManufacturerID < 1 ? null : obj.ManufacturerID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Price", DbType.Double, obj.Price);
            db.AddInParameter(dbCommand, "PriceNew", DbType.Double, obj.PriceNew);
            db.AddInParameter(dbCommand, "ProductCode", DbType.String, obj.ProductCode);
            db.AddInParameter(dbCommand, "ProductName", DbType.String, obj.ProductName);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "Images", DbType.String, obj.Images);
            db.AddInParameter(dbCommand, "IsExist", DbType.Boolean, obj.IsExist);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsVAT", DbType.Boolean, obj.IsVAT);
            db.AddInParameter(dbCommand, "IsHot", DbType.Boolean, obj.IsHot);
            db.AddInParameter(dbCommand, "IsHome", DbType.Boolean, obj.IsHome);
            db.AddInParameter(dbCommand, "IsSellGood", DbType.Boolean, obj.IsSellGood);
            db.AddInParameter(dbCommand, "IsPromotion", DbType.Boolean, obj.IsPromotion);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@ProductID"));
            return -1;
        }

        public static int Product_Update(Product obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Product_Update");
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, obj.ProductID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, obj.CategoryID);
            db.AddInParameter(dbCommand, "ManufacturerID", DbType.Int32, obj.ManufacturerID < 1 ? null : obj.ManufacturerID);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, obj.SortOrder);
            db.AddInParameter(dbCommand, "Price", DbType.Double, obj.Price);
            db.AddInParameter(dbCommand, "PriceNew", DbType.Double, obj.PriceNew);
            db.AddInParameter(dbCommand, "ProductCode", DbType.String, obj.ProductCode);
            db.AddInParameter(dbCommand, "ProductName", DbType.String, obj.ProductName);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            db.AddInParameter(dbCommand, "Images", DbType.String, obj.Images);
            db.AddInParameter(dbCommand, "IsExist", DbType.Boolean, obj.IsExist);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, obj.IsActive);
            db.AddInParameter(dbCommand, "IsVAT", DbType.Boolean, obj.IsVAT);
            db.AddInParameter(dbCommand, "IsHot", DbType.Boolean, obj.IsHot);
            db.AddInParameter(dbCommand, "IsHome", DbType.Boolean, obj.IsHome);
            db.AddInParameter(dbCommand, "IsSellGood", DbType.Boolean, obj.IsSellGood);
            db.AddInParameter(dbCommand, "IsPromotion", DbType.Boolean, obj.IsPromotion);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int Product_Delete(Int32 ProductID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Product_Delete");
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<Product> Product_List(int ProductID, int CategoryID, int IsActive,
            int SortOrder, bool IsHot, bool IsHome, bool IsSellGood, bool IsPromotion, int NumberRow)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Product_List");
            db.AddInParameter(dbCommand, "ProductID", DbType.Int32, ProductID);
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Int32, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            db.AddInParameter(dbCommand, "IsHot", DbType.Boolean, IsHot);
            db.AddInParameter(dbCommand, "IsHome", DbType.Boolean, IsHome);
            db.AddInParameter(dbCommand, "IsSellGood", DbType.Boolean, IsSellGood);
            db.AddInParameter(dbCommand, "IsPromotion", DbType.Boolean, IsPromotion);
            db.AddInParameter(dbCommand, "NumberRow", DbType.Int32, NumberRow);
            List<Product> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Product>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }

        public static List<Product> Product_Paged(int CategoryID, int IsActive,
            int SortOrder, string Keywords, int PageIndex, int PageSize, out int TotalRows)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_Product_Paged");
            db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, CategoryID);
            db.AddInParameter(dbCommand, "IsActive", DbType.Int32, IsActive);
            db.AddInParameter(dbCommand, "SortOrder", DbType.Int32, SortOrder);
            db.AddInParameter(dbCommand, "Keywords", DbType.String, Keywords);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            List<Product> listObj = null;
            TotalRows = 0;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<Product>()).Add(LoadObj(dataReader));
                    TotalRows = MpDataHelp.GetInt(dataReader, "TotalRow");
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}



